<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="Draw Lines - Bimrun Engine v3.7" content="width=device-width, initial-scale=1.0">
    <title>Bimrun Engine v3.7</title>
</head>

<body style=" margin: 0; padding: 0; overflow: hidden; ">

    <div id="container" style=" margin: 0; padding: 0; width: 100%; height: 100%; position: absolute; "></div>
    <div id="desc" style=" margin: 10px; padding: 0px; position: absolute; bottom: 0px; ">左键：添加线段，右键：取消当前线段，每次画的线段点数据保存在points集合中。</div>

    <script src='../depends/config.js'></script>
    <script src='../bimrun-engine-v3.7.js'></script>
    <script>

        let viewer = window.viewer = new Viewer( {

            container: document.getElementById( "container" ), 
            indicator: { url: "../resources/aim.png", size: 50 }

        } );

        viewer.createNavCube( viewer.container );

        viewer.loadScene( '101534881' );


        /**
         * 鼠标点击画线 开始
         * 
         */
        let points = [];
        let currentLine, p1, p2;
        let lookThrough = false; // 设置线段是否需要透视效果

        viewer.addEventListener( BIMRUN.Events.Picking, function ( data ) {

            if ( data.intersect ) {

                viewer.unSelect( [ data.intersect.model ] );

                let point = data.intersect.point;

                if ( p1 ) {

                    // 已经画了第一个点，现在要画第二个点和线段
                    p2 = point.clone();

                    // 将p2加入到最终points列表中
                    points.push( { x: p2.x, y: p2.y, z: p2.z } );

                    p1 = p2;
                    p2 = null;

                } else {

                    // 初始化第一个点
                    p1 = new THREE.Vector3( point.x, point.y, point.z );

                    // 将p1加入到最终points列表中
                    points.push( { x: p1.x, y: p1.y, z: p1.z } );

                }

                // 构造线段的几何图形
                let geometry = new THREE.BufferGeometry().setFromPoints( [ p1, p1 ] );

                currentLine = new THREE.Line( geometry, new THREE.LineBasicMaterial( {
                    color: 0xff00ff,
                    depthTest: !lookThrough
                } ) );

                currentLine.frustumCulled = false;
                currentLine.updateMatrixWorld();

                // 场景中添加自定义物体
                viewer.addCustomObject( currentLine );

                // 打印当前线段点集合
                console.log( points );

            }

        } );

        // 鼠标移动时实时绘制当前线段
        viewer.addEventListener( BIMRUN.Events.MouseMove, function ( data ) {

            let intersect = viewer.pickModel( data.event );

            if ( intersect && currentLine ) {

                let point = intersect.point;

                p2 = new THREE.Vector3().copy( point );

                currentLine.geometry.attributes.position.setXYZ( 1, p2.x, p2.y, p2.z );

                currentLine.geometry.attributes.position.needsUpdate = true;

                viewer.render(); // 强制刷新引擎画面

            }

        } );

        // 右键点击取消当前线段
        viewer.addEventListener( BIMRUN.Events.RightClick, function () {

            viewer.removeCustomObject( currentLine );

            p1 = p2 = currentLine = null;

            points = [];

        } );
        /**
         * 鼠标点击画线 结束
         * 
         */


    </script>
</body>

</html>